package com.ruge.system.controller;

import com.ruge.common.util.RestResponse;
import com.ruge.system.pojo.entity.SysRole;
import com.ruge.system.pojo.request.RoleMenuRequest;
import com.ruge.system.pojo.request.RoleRequest;
import com.ruge.system.service.SysRoleService;
import com.ruge.system.pojo.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ：爱丽丝、如歌
 * @time ：2020/3/16  22:02
 * 描述 ：
 */
@RestController
@RequestMapping("/role")
@Api(value = "角色controller", tags = {"角色操作接口"})
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @GetMapping
    @ApiOperation(value = "获取角色列表信息", notes = "获取角色列表信息")
    public RestResponse list(RoleRequest request) {
        Page<RoleVo> page = sysRoleService.findAll(request);
        return RestResponse.success().setData(page);
    }

    @GetMapping("noPage")
    @ApiOperation(value = "获取角色列表信息", notes = "获取角色列表信息")
    public RestResponse listNoPage(RoleRequest request) {
        List<RoleVo> page = sysRoleService.findAllNoPage(request);
        if (request != null && null != request.getUserId()) {
            List<SysRole> sysRoles = sysRoleService.findRolesByUserId(request.getUserId());
            page.forEach(e -> {
                sysRoles.forEach(e2 -> {
                    if (e.getId().equals(e2.getId())) {
                        e.setLAY_CHECKED(true);
                    }
                });
            });
        }
        return RestResponse.success().setData(page);
    }

    @PostMapping
    @ApiOperation(value = "保存角色详细信息")
    public RestResponse save(@RequestBody SysRole entity) {
        return RestResponse.success().setData(sysRoleService.save(entity));
    }

    @ApiOperation(value = "更新角色详细信息")
    @PutMapping
    public RestResponse update(@RequestBody SysRole entity) {
        return RestResponse.success().setData(sysRoleService.save(entity));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色", notes = "根据url的id来指定删除对象")
    public RestResponse delete(@PathVariable Long id) {
        sysRoleService.deleteById(id);
        return RestResponse.success();
    }

    @ApiOperation(value = "批量操作", notes = "根据url的id来指定批量操作对象")
    @PostMapping("/batch")
    public RestResponse batch(@RequestBody Map<String, List<SysRole>> map) {
        return RestResponse.success().setData(sysRoleService.batch(map));
    }

    @PostMapping("/authMenu")
    @ApiOperation(value = "给角色进行菜单授权详细信息")
    public RestResponse authMenu(@RequestBody List<RoleMenuRequest> entity) {
        sysRoleService.authMenu(entity);
        return RestResponse.success();
    }

    @GetMapping("/getAuthMenu/{roleId}")
    @ApiOperation(value = "给角色进行菜单授权详细信息")
    public RestResponse getAuthMenu(@PathVariable Long roleId) {
        return RestResponse.success().setData(sysRoleService.getAuthMenu(roleId));
    }
}
